package com.zhuang.chapter17.work01;

import java.time.LocalTime;
import java.util.Scanner;

public class BookClient {

	public static void main(String[] args) {
		Book[] books = new Book[3]; // 存储图书

		outBookInfo(books);
		// 购买图书
		Order order = purchase(books);
		// 打印订单
		outOrderInfo(order);
	}

	/**
	 * 
	 * @Title: outBookInfo
	 * @Description: 订购书的方法
	 * @author: KangXiaoZhuang
	 * @param: @param books
	 * @return: void
	 * @throws
	 */
	private static void outBookInfo(Book[] books) {
		books[0] = new Book(1, "Java基础案例编程", 16.9, 100);
		books[1] = new Book(2, "Python数据分析", 26.9, 56);
		books[2] = new Book(3, "Java编程思想", 69.9, 220);

		System.out.println("-------------------\t图书列表\t--------------------");
		System.out.println("编号\tISBN\t名称\t价格\t库存");
		String line = null;
		for (int i = 0; i < books.length; i++) {
			line = books[i].getId() + "\t" + books[i].getName() + "\t" + books[i].getPrice() + "\t"
					+ books[i].getStorage();
			System.out.println(line);
		}
		System.out.println("------------------------------------------------");
	}

	/**
	 * 
	 * @Title: purchase
	 * @Description: 购买图书的方法
	 * @author: KangXiaoZhuang
	 * @param: @param  books
	 * @param: @return
	 * @return: Order
	 * @throws
	 */
	private static Order purchase(Book[] books) {
		Scanner sc = new Scanner(System.in);
		OrderItem item = null;

		Order order = new Order("book-order-" + LocalTime.now().toString());
		for (int i = 0; i < books.length; i++) {
			System.out.println("请选择需要购买的图书编号：");
			int bno = sc.nextInt();
			System.out.println("购买数量：");
			int num = sc.nextInt();
			item = new OrderItem(books[bno - 1], num);
			order.setItems(item, i);
			System.out.println("请继续购买图书。");
		}
		sc.close();
		return order;
	}

	/**
	 * 
	 * @Title: outOrderInfo
	 * @Description: 处理订单信息
	 * @author: KangXiaoZhuang
	 * @param: @param order
	 * @return: void
	 * @throws
	 */
	private static void outOrderInfo(Order order) {
		System.out.println("-------------------\t图书订单\t--------------------");
		System.out.println("订单编号：" + order.getOid());
		System.out.println("\t图书名称\t\t购买数量\t图书单价");
		OrderItem[] items = order.getItems();
		for (int i = 0; i < items.length; ++i) {
			System.out.println(
					items[i].getBook().getName() + "\t" + items[i].getNums() + "\t" + items[i].getBook().getPrice());
		}
		System.out.println("订单总额：￥" + order.getTotal());
		System.out.println("------------------------------------------------");
	}

}
